<?php
/**
 * 基础协议包
 */

abstract class protocol{
	protected $serverreturn;
	protected $templateName;//默认的模板
	protected $logmessage; //日志记录信息
	
	protected $xmlHeader;//xmlHeader
	protected $xmlBody;		//请求的XML BODY
	protected $xmlRequest;	//需要发出去的XML请求包
	protected $xmlUncodeBody;//未加密的BODY
	
	//协议实现接口地址 - 此常量仅仅跟随本plus 不适合置放在 config 级
	const Host = "";	//请求主机地址
	const Port = "";    //请求主机端口
	const interf = "";	//请求接口
	
	const protocolTemplate = "protocolTemplate";	//协议调用模板
	
	public function __construct(){
		$this->templateName = self::protocolTemplate;
	}
	
	abstract protected function xmlRquest($templateName);//根据模板构造请求
	abstract protected function send();//发送请求
	abstract protected function getResponse();	//提取返回数据
	
	protected function xmlBody($param){ // 构造请求头信息
		$string ="<B>";
		$string .= $this->xmlEncode($param);
		$string .= "</B>";
		$this->xmlBody = $string;		
	}
	
	protected function xmlEncode($param){
		$string ='<?xml version="1.0" encoding=="UTF-8"?>';
		$string ='<B>';
		if(is_array($param)){
			foreach($param as $key=>$value){
				if(is_numeric(($key)))continue;
				$string .= "<$key><![CDATA[$value]]></$key>";
			}
		}
		$string .= "</B>";
		$this->xmlUncodeBody = $string; //保存未加密数据
		$this->xmlBody = PACKNODE::ENCODE($string,PACKNODE::getKey($db,$snumber));
	}
	
	protected function xmlHeader($param){ // 构造请求头信息	
		$string ="<H>";
		if(is_array($param)){
			foreach($param as $key=>$value){
				if(is_numeric(($key)))continue;
				$string .= "<$key><![CDATA[$value]]></$key>";
			}
		}
		$string .= "</H>";
		$this->xmlHeader = $string;
		
	}
	
	//记录所有请求日志信息
	public function  __destruct(){		
	}
}

//采用CURL模块开发实现协议通信
class curlProtocol extends protocol {
	public function xmlRquest($templateName);//根据模板构造请求
	public function send();//发送请求
	public function getResponse();	//提取返回数据
}


//采用SOCKET模块开发实现通信协议
class socketProtocol extends protocol {
	
}


/**
 * 数据加解密
 *
 */
class PACKNODE
{
	public function ENCODE($text,$key)
	{
		//将节点信息包装
		$dc = MCRYPT_DES;
		$v = MCRYPT_MODE_ECB;
		$block_size = mcrypt_get_block_size ($dc, $v);
     	$key_size = mcrypt_get_key_size($dc, $v);
	    $iv_size = mcrypt_get_iv_size($dc, $v);
	    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
	    if ( strlen($key) > $key_size) $key = substr( $key, 0, $key_size);
	    $crypttext = mcrypt_encrypt($dc, $key, $text, $v, $iv);
		return base64_encode($crypttext);		
	}
	
	public function DECODE($content,$key)
	{
		$dc = MCRYPT_DES;
		$v = MCRYPT_MODE_ECB;		
		$decoded = base64_decode($content);
		$iv = mcrypt_create_iv(mcrypt_get_iv_size($dc, $v), MCRYPT_RAND);
		$decrypted = mcrypt_decrypt($dc, $key, $decoded, $v, $iv);
		return $decrypted;
	}
	
	public function getKey(&$db,$snumber)
	{
		$row = $db->select("terminal","secretkey",array('serialnumber'=>$snumber));
		return $rv[0];
	}
}

//基础应用
class appprotocol{
	public $REQUEST;
	
	public function __construct(){
		if(function_exists("curl_init")){
			$this->REQUEST = new curlProtocol();
		}
		elseif (function_exists("socket_create")){
			$this->REQUEST = new socketProtocol();
		}
	}
}


return md5(__FILE__);